/*
 * JBoss, Home of Professional Open Source
 * Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package za.ac.nmmu.muhelp.comms;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.HashMap;
import java.util.logging.Logger;

import java.net.URLEncoder;

//
// these imports are for XMPP
//
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.packet.Presence;;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

//
// JSON imports
//
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;


import za.ac.nmmu.muhelp.model.XMPPConfiguration;
import za.ac.nmmu.muhelp.data.XMPPConfigurationRepository;

import za.ac.nmmu.muhelp.util.HTTPUtilities;

public class XMPPListenerThread extends Thread 
		implements MessageListener, ChatManagerListener {

	Map<String, Chat> chats = new HashMap<String, Chat>();

	Boolean keepGoing = Boolean.TRUE;

	HTTPUtilities httpUtilities;

public XMPPListenerThread() {
	super();
}

public void chatCreated(Chat chat, boolean createdLocal) {
	try {
		Chat tmp = chats.get(chat.getParticipant());
		if ( tmp == null ) {
			System.out.println("adding " + chat.getParticipant());
			chats.put(chat.getParticipant(), chat);
			chat.addMessageListener(this);
		}
	}
	catch ( Throwable t ) {
		System.out.println("chatCreated throwing " + t);
	}
}

public void processMessage(Chat chat, Message message) {
  	try {
                if( (message.getType() == Message.Type.chat) &&
                        (message.getBody() != null ) &&
                        (message.getBody().length() > 0 ) ) {

                        System.out.println(chat.getParticipant() + " says: " +
                                message.getBody() + " thread " + message.getThread());
			System.out.println(message.toXML());

			String url = new String(
				"http://localhost:8080/muhelp-core/rest/receive");

			ObjectMapper objectMapper = new ObjectMapper();
			String payload = objectMapper.writeValueAsString(message);
			System.out.println("payload is " + payload);

			String returnValues = httpUtilities.putPage(url, payload);
			System.out.println("returnValues "  + returnValues);

			
		}
	}
	catch ( Throwable t ) {
		System.out.println("throwing " + t);
	}

}

public void stopThread() {
	System.out.println("setting keepGoing to False");
	keepGoing = Boolean.FALSE;
}

public void run() {
	while ( keepGoing ) {
		try {
			System.out.println("sleeping...");
			sleep(100000);
		}
		catch ( Throwable t ) {
			System.out.println("sleep throwing " + t);
		}
	}
	System.out.println("Thread finishing");
}

}
